package com.blog.admin.service;

import com.blog.admin.mapper.BlogResourcesMapper;
import com.blog.admin.mapper.BlogClassifyMapper;
import com.blog.admin.mapper.BlogCommentMapper;
import com.blog.admin.vo.BlogResourcesVO;
import com.blog.admin.vo.BlogClassifyVO;
import com.blog.admin.vo.BlogCommentVO;
import com.blog.common.PageHelperLog;
import com.blog.vo.PageVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * @author tangqj
 * @Description 资源和点点滴滴模块
 * @create 2020/2/18 14:35
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BlogResourcesService {
    @Autowired
    BlogResourcesMapper blogResourcesMapper;
    @Autowired
    BlogClassifyMapper blogClassifyMapper;
    @Autowired
    BlogCommentMapper blogCommentMapper;
    /**创建线程池*/
    public static ScheduledExecutorService service = Executors.newScheduledThreadPool(50);


    /**
     * 分页获取文章
     *
     * @param page
     * @return
     */
    @PageHelperLog(desc = "msg")
    public PageInfo<BlogResourcesVO> getBlogResources(PageVO page, BlogResourcesVO blogResourcesVO) {
        List<BlogResourcesVO> list = blogResourcesMapper.getBlogResources(blogResourcesVO);
        PageInfo<BlogResourcesVO> pageRest = new PageInfo<BlogResourcesVO>(list);
        return pageRest;
    }

 /**
     * 分页获取文章
     *
     * @param page
     * @return
     */
    @PageHelperLog(desc = "msg")
    public PageInfo<BlogResourcesVO> getBlogTime(PageVO page, BlogResourcesVO blogResourcesVO) {
        List<BlogResourcesVO> list = blogResourcesMapper.getBlogTime(blogResourcesVO);
        PageInfo<BlogResourcesVO> pageRest = new PageInfo<BlogResourcesVO>(list);
        return pageRest;
    }



    /**
     * 插入文章
     *
     * @param blogResourcesVO
     * @return
     */
    public boolean insertBlogResources(BlogResourcesVO blogResourcesVO) {
        int result = blogResourcesMapper.insertBlogResources(blogResourcesVO);
        return result > 0 ? true : false;
    }

    /**
     * 修改文章
     *
     * @param blogResourcesVO
     * @return
     */
    public boolean updateBlogResources(BlogResourcesVO blogResourcesVO) {
        int result = blogResourcesMapper.updateBlogResources(blogResourcesVO);
        return result > 0 ? true : false;
    }


    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    public boolean deleteBlogResourcesById(String id) {
        try {
            int result = blogResourcesMapper.deleteBlogResourcesById(id);
            if (result < 1) {
                return false;
            }
            return result > 0 ? true : false;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//关键 手动回滚
            return false;
        }

    }



}
